package ar.activity;

import java.util.ArrayList;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import ar.Location.Weather.ForecastConditions;
import ar.Location.Weather.Weather;
import ar.activity.R.drawable;

public class WeatherForecastActivity extends BaseActivity {
	private final char degree = (char) 0x00B0;

	public static Weather weather = null;

	public int convertFtoC(String fahrenheit) {
		int fah = Integer.parseInt(fahrenheit);
		fah = fah - 32;
		fah = fah / 9;
		fah = fah * 5;
		return fah;
	}

	public int convertCtoF(String celsius) {
		int cel = Integer.parseInt(celsius);
		cel = cel * 9;
		cel = cel / 5;
		cel = cel + 32;
		return cel;
	}

	ProgressDialog progressDialog = null;

	TextView txtViewCityName = null;
	TextView txtViewForecastDate = null;
	TextView txtViewCurrentCondition = null;
	TextView txtViewtemperature = null;
	TextView txtViewHumidity = null;
	TextView txtViewWindCondition = null;

	TextView txtViewday1 = null;
	TextView txtViewLow1 = null;
	TextView txtViewHigh1 = null;
	TextView txtViewCondition1 = null;

	TextView txtViewday2 = null;
	TextView txtViewLow2 = null;
	TextView txtViewHigh2 = null;
	TextView txtViewCondition2 = null;

	TextView txtViewday3 = null;
	TextView txtViewLow3 = null;
	TextView txtViewHigh3 = null;
	TextView txtViewCondition3 = null;

	TextView txtViewday4 = null;
	TextView txtViewLow4 = null;
	TextView txtViewHigh4 = null;
	TextView txtViewCondition4 = null;

	ImageView imgViewCurrentCondition = null;
	ImageView imgViewCondition1 = null;
	ImageView imgViewCondition2 = null;
	ImageView imgViewCondition3 = null;
	ImageView imgViewCondition4 = null;

	Handler handlerUpdateWeather = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.about);
		super.onCreate(savedInstanceState);
		tvWhatScreen.setText("Weather");

		if (weather != null) {
			setContentView(R.layout.weather_layout);
			initView();
			updateWeatherUI(weather);
		} else {
			setContentView(R.layout.weather_no_data_layout);
		}
	}

	public void initHandler() {
		handlerUpdateWeather = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				if (msg != null) {
					final Weather weather = (Weather) msg.obj;
					if (weather != null) {
						updateWeatherUI(weather);
						progressDialog.dismiss();
					}
				}
			}
		};
	}

	private void initView() {
		// TODO Auto-generated method stub
		txtViewCityName = (TextView) findViewById(R.id.txtViewCityName);
		txtViewForecastDate = (TextView) findViewById(R.id.txtViewForecastDate);
		txtViewCurrentCondition = (TextView) findViewById(R.id.txtViewCurrentCondition);
		txtViewtemperature = (TextView) findViewById(R.id.txtViewTemperature);
		txtViewHumidity = (TextView) findViewById(R.id.txtViewHumidity);
		txtViewWindCondition = (TextView) findViewById(R.id.txtViewWindCondition);
		imgViewCurrentCondition = (ImageView) findViewById(R.id.imgViewCondition);

		txtViewday1 = (TextView) findViewById(R.id.txtViewDay1);
		txtViewLow1 = (TextView) findViewById(R.id.txtViewLow1);
		txtViewHigh1 = (TextView) findViewById(R.id.txtViewHigh1);
		txtViewCondition1 = (TextView) findViewById(R.id.txtViewCondition1);
		imgViewCondition1 = (ImageView) findViewById(R.id.imgViewCondition1);

		txtViewday2 = (TextView) findViewById(R.id.txtViewDay2);
		txtViewLow2 = (TextView) findViewById(R.id.txtViewLow2);
		txtViewHigh2 = (TextView) findViewById(R.id.txtViewHigh2);
		txtViewCondition2 = (TextView) findViewById(R.id.txtViewCondition2);
		imgViewCondition2 = (ImageView) findViewById(R.id.imgViewCondition2);

		txtViewday3 = (TextView) findViewById(R.id.txtViewDay3);
		txtViewLow3 = (TextView) findViewById(R.id.txtViewLow3);
		txtViewHigh3 = (TextView) findViewById(R.id.txtViewHigh3);
		txtViewCondition3 = (TextView) findViewById(R.id.txtViewCondition3);
		imgViewCondition3 = (ImageView) findViewById(R.id.imgViewCondition3);

		txtViewday4 = (TextView) findViewById(R.id.txtViewDay4);
		txtViewLow4 = (TextView) findViewById(R.id.txtViewLow4);
		txtViewHigh4 = (TextView) findViewById(R.id.txtViewHigh4);
		txtViewCondition4 = (TextView) findViewById(R.id.txtViewCondition4);
		imgViewCondition4 = (ImageView) findViewById(R.id.imgViewCondition4);
	}

	public void updateWeatherUI(Weather weather) {
		if (weather != null) {
			txtViewCityName.setText(weather.forecastInformation.city);
			txtViewForecastDate
					.setText(weather.forecastInformation.forecastDate);
			txtViewCurrentCondition
					.setText(weather.currentConditions.condition);
			txtViewtemperature.setText(weather.currentConditions.temperature
					+ degree + "C");
			txtViewHumidity.setText(weather.currentConditions.humidity);
			txtViewWindCondition
					.setText(weather.currentConditions.windCondition);
			int resId = getResId(weather.currentConditions.condition);
			if (resId == -1) {
				imgViewCurrentCondition.setImageBitmap(null);
			} else {
				imgViewCurrentCondition.setImageResource(resId);
			}

			if (weather.listConditions != null) {
				ArrayList<ForecastConditions> listConditions = weather.listConditions;
				int size = listConditions.size();
				if (size > 0) {
					ForecastConditions condition = listConditions.get(0);
					if (condition != null) {
						txtViewday1.setText(condition.dayOfWeek);
						txtViewLow1.setText("Low: "
								+ convertFtoC(condition.lowTemperature)
								+ degree + "C");
						txtViewHigh1.setText("High: "
								+ convertFtoC(condition.highTemperature)
								+ degree + "C");
						txtViewCondition1.setText(condition.condition);
						resId = getResId(condition.condition);
						if (resId == -1) {
							imgViewCondition1.setImageBitmap(null);
						} else {
							imgViewCondition1.setImageResource(resId);
						}
					}

					if (size > 1) {
						condition = listConditions.get(1);
						if (condition != null) {
							txtViewday2.setText(condition.dayOfWeek);
							txtViewLow2.setText("Low: "
									+ convertFtoC(condition.lowTemperature)
									+ degree + "C");
							txtViewHigh2.setText("High: "
									+ convertFtoC(condition.highTemperature)
									+ degree + "C");
							txtViewCondition2.setText(condition.condition);
							resId = getResId(condition.condition);
							if (resId == -1) {
								imgViewCondition2.setImageBitmap(null);
							} else {
								imgViewCondition2.setImageResource(resId);
							}
						}
					}

					if (size > 2) {
						condition = listConditions.get(2);
						if (condition != null) {
							txtViewday3.setText(condition.dayOfWeek);
							txtViewLow3.setText("Low: "
									+ convertFtoC(condition.lowTemperature)
									+ degree + "C");
							txtViewHigh3.setText("High: "
									+ convertFtoC(condition.highTemperature)
									+ degree + "C");
							txtViewCondition3.setText(condition.condition);
							resId = getResId(condition.condition);
							if (resId == -1) {
								imgViewCondition3.setImageBitmap(null);
							} else {
								imgViewCondition3.setImageResource(resId);
							}
						}
					}

					if (size > 3) {
						condition = listConditions.get(3);
						if (condition != null) {
							txtViewday4.setText(condition.dayOfWeek);
							txtViewLow4.setText("Low: "
									+ convertFtoC(condition.lowTemperature)
									+ degree + "C");
							txtViewHigh4.setText("High: "
									+ convertFtoC(condition.highTemperature)
									+ degree + "C");
							txtViewCondition4.setText(condition.condition);
							resId = getResId(condition.condition);
							if (resId == -1) {
								imgViewCondition4.setImageBitmap(null);
							} else {
								imgViewCondition4.setImageResource(resId);
							}
						}
					}
				}
			}
		}
	}

	public int getResId(String condition) {
		if (condition.toLowerCase().equals("cloudy")) {
			return drawable.cloudy;
		}
		if (condition.toLowerCase().equals("chance of rain")) {
			return drawable.chance_of_rain;
		}
		if (condition.toLowerCase().equals("chance of snow")) {
			return drawable.chance_of_snow;
		}
		if (condition.toLowerCase().equals("chance of storm")) {
			return drawable.chance_of_storm;
		}
		if (condition.toLowerCase().equals("dust")) {
			return drawable.dust;
		}
		if (condition.toLowerCase().equals("fog")) {
			return drawable.fog;
		}
		if (condition.toLowerCase().equals("haze")) {
			return drawable.haze;
		}
		if (condition.toLowerCase().equals("icy")) {
			return drawable.icy;
		}
		if (condition.toLowerCase().equals("mist")) {
			return drawable.mist;
		}
		if (condition.toLowerCase().equals("mostly cloudy")) {
			return drawable.mostly_cloudy;
		}
		if (condition.toLowerCase().equals("mostly sunny")) {
			return drawable.mostly_sunny;
		}
		if (condition.toLowerCase().equals("partly cloudy")) {
			return drawable.partly_cloudy;
		}
		if (condition.toLowerCase().equals("partly sunny")) {
			return drawable.partly_sunny;
		}
		if (condition.toLowerCase().equals("rain")) {
			return drawable.rain;
		}
		if (condition.toLowerCase().equals("sleet")) {
			return drawable.sleet;
		}
		if (condition.toLowerCase().equals("smoke")) {
			return drawable.smoke;
		}
		if (condition.toLowerCase().equals("snow")) {
			return drawable.snow;
		}
		if (condition.toLowerCase().equals("storm")) {
			return drawable.storm;
		}
		if (condition.toLowerCase().equals("thunderstorm")) {
			return drawable.storm;
		}
		if (condition.toLowerCase().equals("sunny")
				|| condition.toLowerCase().equals("sun")) {
			return drawable.sunny;
		}
		if (condition.toLowerCase().contains("showers")) {
			return drawable.showers;
		}
		if (condition.toLowerCase().equals("overcast")) {
			return drawable.overcast;
		}
		if (condition.toLowerCase().equals("clear")) {
			return drawable.clear;
		}
		return -1;
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		finish();
	}
}